﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;

namespace DAL
{
    public class UsersInfoServices
    {
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="userinfo"></param>
        /// <returns>bool</returns>
        public static bool UsersAdd(UsersInfo userinfo)
        {
            //执行添加数据处理
            string sql = string.Format("insert into users values('{0}','{1}','{2}','{3}','{4}')", userinfo.userName, userinfo.pwd, userinfo.address, userinfo.phone, userinfo.email);
            return DBHelper.ExecuteNonQuery(sql);
        }
        /// <summary>
        /// 全部用户查询
        /// </summary>
        /// <returns>List<UsersInfo></returns>
        public static List<UsersInfo> UserSelects()
        {
            //执行全部用户查询数据处理
            string sql = "select * from users";
            List<UsersInfo> list = new List<UsersInfo>();
            DataTable dt = DBHelper.GetDataTable(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                UsersInfo U = new UsersInfo
                {
                    usersId = Convert.ToInt32(dt.Rows[i]["userId"]),
                    userName = dt.Rows[i]["userName"].ToString(),
                    pwd = dt.Rows[i]["pwd"].ToString(),
                    address = dt.Rows[i]["uaddress"].ToString(),
                    phone = dt.Rows[i]["phone"].ToString(),
                    email = dt.Rows[i]["email"].ToString(),

                };
                list.Add(U);
            }
            return list;
        }
        /// <summary>
        /// 根据用户名查询--查重
        /// </summary>
        /// <param name="userName"></param>
        /// <returns>bool</returns>
        public static bool UserAddSelect(string userName)
        {
            //执行查重数据处理
            string sql = string.Format("select * from users where userName='{0}'", userName);
            bool b = false;
            DataTable dt = DBHelper.GetDataTable(sql);
            if (dt.Rows.Count > 0)
            {
                b = true;
            }
            return b;
        }
        /// <summary>
        /// 根据用户账号、密码查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns>返回一个用户的数据</returns>
        public static bool UserLogin(string name, string pwd)
        {
            //执行单一用户查询数据处理
            string sql = string.Format("select * from users where userName='{0}' and pwd='{1}'", name, pwd);
            bool b = false;
            DataTable dt = DBHelper.GetDataTable(sql);
            if (dt.Rows.Count > 0)
            {
                b = true;
            }
            return b;
        }
    }
}
